import { registerEntityType, requestModel } from '../base'
import { registerEntityTable } from '../table'
import { RoutingType, type Routing } from './types'
import { initRoutingIndex } from './routing-index'
import { initMap } from './map'
import { initRoutingMapItems } from './map-item'
import { setupRoutingInfos } from './info/routing-info'
import { registerAppMenu } from '@/app'
import { selectAndShowAirportRoutingTable, selectAndShowWaypointRoutingTable } from './util'
import RoutingFilterVue from './RoutingFilter.vue'

export * from './types'
export * from './feature'
export { type RoutingInfo, type RoutingSegmentInfo, getRoutingInfo } from './info'
export { routingMapItem } from './map-item'
export * from './util'
export { getAirportRoutingSet, getWaypointRoutingSet } from './routing-index'

function setupRoutingTable() {
  registerEntityTable<Routing>({
    type: 'Routing',
    columns: [
      {
        name: 'id',
        cell: (e) => e.id,
        width: 100
      },
      {
        name: '起飞机场',
        cell: (e) => e.departureAirport || '',
        width: 100
      },
      {
        name: '落地机场',
        cell: (e) => e.arrivalAirport || '',
        width: 100
      },
      {
        name: '起始航路点',
        cell: (e) => e.waypointList[0],
        width: 100
      },
      {
        name: '结束航路点',
        cell: (e) => e.waypointList[e.waypointList.length - 1],
        width: 100
      },
      {
        name: '航路点数',
        cell: (e) => e.waypointList.length,
        width: 100
      }
    ],
    filter: RoutingFilterVue
  })
}

export async function setupRouting() {
  const data = (await requestModel(RoutingType)) as Routing[]
  registerEntityType<Routing>({
    type: 'Routing',
    typeName: '飞行路线',
    data
  })
  setupRoutingInfos(data)
  setupRoutingTable()
  initRoutingIndex(data)
  initMap()
  initRoutingMapItems()

  registerAppMenu({
    path: '模型/wpt:{mas-icon-Routing}航路点对应飞行路线',
    onClick: selectAndShowWaypointRoutingTable
  })
  registerAppMenu({
    path: '模型/apt:{mas-icon-Routing}机场对应飞行路线',
    onClick: selectAndShowAirportRoutingTable
  })
}
